<script>
export default {
  name: 'ImprovementSuggestion',
  props: {
    projectList: {
      required: false,
      default: []
    },
    queryForm: {
      required: false,
      default: {}
    },
    startTime: {
      type: String,
      required: false
    },
    endTime: {
      type: String,
      required: false
    },
    focusEnergyDataList: {
      type: Array,
      default: []
    },
    electricalEnergyDataList: {
      type: Array,
      default: []
    }
  },
  computed: {
    getProjectName() {
      let sceneProjectId = this.queryForm.sceneProjectId
      if (sceneProjectId == null && this.projectList.length > 0) {
        sceneProjectId = this.projectList[0].value
      }
      return this.projectList.find(project => project.value === sceneProjectId)?.label
    },
    getMaxFocusEnergyData() {
      if (this.focusEnergyDataList.length > 0) {
        return this.focusEnergyDataList.slice().sort((a, b) => b.value - a.value)[0]
      }
      return {}
    },
    getMinFocusEnergyData() {
      if (this.focusEnergyDataList.length > 0) {
        return this.focusEnergyDataList.slice().sort((a, b) => b.value - a.value)[0]
      }
      return {}
    },
    getAvgElectricalEnergy() {
      if (this.electricalEnergyDataList.length > 0) {
        return this.electricalEnergyDataList.map(item => item.value).reduce((accumulator, currentValue) => accumulator + currentValue, 0) / this.electricalEnergyDataList.length
      }
      return 0
    },
    getMaxElectricalEnergy() {
      if (this.electricalEnergyDataList.length > 0) {
        return this.electricalEnergyDataList.slice().sort((a, b) => b.value - a.value)[0]
      }
      return {}
    },
    getMinElectricalEnergy() {
      if (this.electricalEnergyDataList.length > 0) {
        return this.electricalEnergyDataList.slice().sort((a, b) => b.value - a.value)[0]
      }
      return {}
    }
  },
  data() {
    return {
      content: null
    }
  },
  created() {
    this.getConfigContent();
  },
  methods: {
    async getConfigContent() {
      const avg = this.getAvgElectricalEnergy
      let name = ''
      if (avg >= 95) {
        name = '设备能效高于等于95%'
      } else if (avg >= 70) {
        name = '设备能效处于70%-95%'
      } else {
        name = '设备能效低于70%'
      }
      const response = await this.$api.optimizationSuggestion.getContent({
        name: name
      })
      this.content = response.data
    }
  }
}
</script>

<template>
  <div>
    <h2>04、优化建议</h2>
    <p>{{ getProjectName }} 在{{ startTime }}- {{ endTime }}中，能耗总量为{{ focusEnergyDataList.map(item => item.value)
      .reduce((accumulator, currentValue) => accumulator + currentValue, 0) }}kgce，其中{{ getMaxFocusEnergyData.name
      }}的能耗量最高，为{{ getMaxFocusEnergyData.value }}kgce
      ，{{ getMinFocusEnergyData.name
      }}的能耗量最低，为{{ getMinFocusEnergyData.value
      }} kgce。
      总用电设备的平均能效为{{ getAvgElectricalEnergy }}%，其中{{ getMaxElectricalEnergy.name }}
      的平均能效最高，为{{ getMaxElectricalEnergy.value }} %；
      {{ getMinElectricalEnergy.name }}的平均能效最低，为{{ getMinElectricalEnergy.value }} %。</p>
    <div>
      <p>优化建议：</p>
      <p class="content" v-html="content"></p>
    </div>
  </div>
</template>

<style scoped lang="scss">
p {
  text-indent: 2em;
}

.content {
  white-space: pre-wrap;
  line-height: 28px;
  text-indent: 0;
  padding-left: 4%;
}
</style>
